﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OpenSIS.OpenSISWCFServiceReference;
using AjaxControlToolkit;
using System.Globalization;
using OSISToolTip;

namespace OpenSIS.Tool
{
    public partial class Ethnicity : System.Web.UI.Page
    {
        protected string sCulture;
        protected void Page_Load(object sender, EventArgs e)
        {
            TooltipLang TTL = new TooltipLang();
            ModalPopupExtender2.Hide();
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                    TTL.tooltip();
            
                    imgbtnEdit.Attributes.Add("onmouseover", "toolTip('" + TTL.Edit + "')");
                    imgbtnEdit.Attributes.Add("onmouseout", "toolTip('')");
                    imgbtnEdit.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");

                    imgbtnAdd.Attributes.Add("onmouseover", "toolTip('" + TTL.Add + "')");
                    imgbtnAdd.Attributes.Add("onmouseout", "toolTip('')");
                    imgbtnAdd.Attributes.Add("onclick", "toolTip('')");

                    //imgbtnActive.Attributes.Add("onmouseover", "toolTip('" + TTL.Active + "')");
                    //imgbtnActive.Attributes.Add("onmouseout", "toolTip()");
                    //imgbtnActive.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");

                    //imgbtnInActive.Attributes.Add("onmouseover", "toolTip('" + TTL.InActive + "')");
                    //imgbtnInActive.Attributes.Add("onmouseout", "toolTip()");
                    //imgbtnInActive.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");
                    imgbtnDelete.Attributes.Add("onmouseover", "toolTip('" + TTL.Delete + "')");
                    imgbtnDelete.Attributes.Add("onmouseout", "toolTip()");
                    imgbtnDelete.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");
                }
               
                if (!Page.IsPostBack)
                {
                   

                   
                    PopulateGridview();
                   
                    //PopulateNextGrade(ref ddlNextGradeAdd);

                }
            
        }

        //**************
        protected void PopulateGridview()
        {
            DataTable dt = new DataTable();


            OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();

            dt = Tools.PopulateEthnicityDetails("", "", Convert.ToInt32(Session["SchoolID"]));

            Tools.Close();

            string Culture = System.Web.HttpContext.Current.Session["MLCulture"].ToString();

            if (dt.Rows.Count > 0)
            {

                rlEthnicity.DataSource = dt;
                rlEthnicity.DataBind();

                //*******************************6th August//
                //lblmsg.Text = GVEthnicity.Items.Count.ToString() + " " + "Grade Level were found";


                switch (Culture)
                {
                    case "en-US":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Ethnicity were found";
                        break;
                    case "es-MX":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Etnia se encontraron";
                        break;
                    case "zh-CN":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "族群被发现";
                        break;
                    case "ja-JP":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "民族が見つかりませんでした";
                        break;
                    case "de-DE":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Volkstum gefunden";
                        break;
                    case "ko-KR":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "민족성이 발견";
                        break;
                    case "fr-FR":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "L'ethnicité ont été trouvés";
                        break;
                    case "it-IT":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Etnia sono stati trovati";
                        break;
                    case "ru-RU":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Этничность были найдены";
                        break;
                    case "pt-BR":
                        lblmsg.Text = rlEthnicity.Items.Count.ToString() + " " + "Etnia foram encontrados";
                        break;
                    //case "ar-SA":
                    //    lblmsg.Text = GVEthnicity.Items.Count.ToString() + " " + "تم العثور على الرتبة";
                    //    break;
                }
                //    //*******************************6th August//


            }
            else
            {


                rlEthnicity.DataBind();

                //    //*******************************6th August//
                //    //lblmsg.Text = "No Grade Level were found";
                switch (Culture)
                {
                    case "en-US":
                        lblmsg.Text = "No Ethnicity were found";
                        break;
                    case "es-MX":
                        lblmsg.Text = "No se encontraron Etnia";
                        break;
                    case "zh-CN":
                        lblmsg.Text = "没有种族被发现";
                        break;
                    case "ja-JP":
                        lblmsg.Text = "いいえ民族は見つかりませんでした";
                        break;
                    case "de-DE":
                        lblmsg.Text = "Nr. Volkstum gefunden";
                        break;
                    case "ko-KR":
                        lblmsg.Text = "아니 인종이 발견되었습니다";
                        break;
                    case "fr-FR":
                        lblmsg.Text = "Aucun ethnicité ont été trouvés";
                        break;
                    case "it-IT":
                        lblmsg.Text = "Non sono stati trovati Etnia";
                        break;
                    case "ru-RU":
                        lblmsg.Text = "Этничность не были найдены";
                        break;
                    case "pt-BR":
                        lblmsg.Text = "Nenhum Étnico foram encontrados";
                        break;
                    //case "ar-SA":
                    //    lblmsg.Text = "لا الرتبة تم العثور على";
                    //    break;
                }
                //    //*******************************6th August//

            }
        }


        protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
        {
            
            try
            {
                foreach (ReorderListItem RI in rlEthnicity.Items)
                {

                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {
              
                    
                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        if (chk.Checked)
                        {

                            HiddenField hdnEthnicityId = (HiddenField)RI.FindControl("hdnEthnicityId");
                            Label lblEthnicityName = (Label)RI.FindControl("lblEthnicityName");
                            Label lblEthnicityShortName = (Label)RI.FindControl("lblEthnicityShortName");


                            txtEthnicityEdit.Text = lblEthnicityName.Text.ToString();
                            txtShortNameEdit.Text = lblEthnicityShortName.Text.ToString();



                            Session["EthnicityId"] = hdnEthnicityId.Value.ToString();

                        }

                    }
                }


            }
            catch
            {

            }

            ModalPopupExtender2.Enabled = true;
            ModalPopupExtender2.Show();
        }

      

        protected void btnSave_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();
            try
            {
                Tools.AddEthinicity(txtShortNameAdd.Text.ToString(), txtEthnicityAdd.Text.ToString(), DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo), Convert.ToInt32(Session["SchoolID"]));


                PopulateGridview();
            }
            catch
            {
            }
            finally
            {
                txtShortNameAdd.Text = "";
                txtEthnicityAdd.Text = "";
                Tools.Close();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                PopulateGridview();
                txtShortNameAdd.Text = "";
                txtEthnicityAdd.Text = "";
                lblerrmsg.Text = "";
            }
            catch
            {
            }
        }



        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();
            try
            {

                Tools.UpdateEthinicity(txtShortNameEdit.Text.ToString(), txtEthnicityEdit.Text.ToString(), DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo), Convert.ToInt32(Session["EthnicityId"]));
               
                
                PopulateGridview();

            }
            catch
            {
            }
            finally
            {
                ModalPopupExtender2.Enabled = false;
                Tools.Close();
            }

        }

        protected void btnCancelEdit_Click(object sender, EventArgs e)
        {
            try
            {
                ModalPopupExtender2.Enabled = false;
                PopulateGridview();
            }
            catch
            {
            }
        }

        private void ml_InitCulture()
        {
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                String MLCultureName = (String)Session["MLCulture"];
                sCulture = Session["MLCulture"].ToString();
                String MLUICultureName = (String)Session["MLUICulture"];

                if ((MLCultureName != null) && (MLCultureName.Length > 0))
                {
                    Page.Culture = MLCultureName;
                }

                if ((MLUICultureName != null) && (MLUICultureName.Length > 0))
                {
                    Page.UICulture = MLUICultureName;
                }
            }
        }



        protected override void InitializeCulture()
        {
            ml_InitCulture();
        }

        

        //protected void imgbtnInActive_Click(object sender, ImageClickEventArgs e)
        //{
        //    OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();
        //    try
        //    {
        //         foreach (ReorderListItem RI in rlEthnicity.Items)
        //        {

        //            if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
        //            {


        //                CheckBox chk = (CheckBox)RI.FindControl("chkrow");
        //                if (chk.Checked)
        //                {

        //                    HiddenField hdnEthnicityId = (HiddenField)RI.FindControl("hdnEthnicityId");

        //                    Tools.UpdateEthnicityFlag("I", Convert.ToInt32(hdnEthnicityId.Value), Convert.ToInt32(Session["SchoolID"]));

        //                }

        //            }
        //        }
        //         PopulateGridview();
        //    }
        //    catch
        //    {
        //    }
        //    finally
        //    {
        //        Tools.Close();
        //    }
        //}

        //protected void imgbtnActive_Click(object sender, ImageClickEventArgs e)
        //{
        //    OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();
        //    try
        //    {
        //          foreach (ReorderListItem RI in rlEthnicity.Items)
        //        {

        //            if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
        //            {


        //                CheckBox chk = (CheckBox)RI.FindControl("chkrow");
        //                if (chk.Checked)
        //                {
        //                    HiddenField hdnEthnicityId = (HiddenField)RI.FindControl("hdnEthnicityId");

        //                    Tools.UpdateEthnicityFlag("A", Convert.ToInt32(hdnEthnicityId.Value), Convert.ToInt32(Session["SchoolID"]));

        //                }
        //            }

        //        }

        //          PopulateGridview();
        //    }
        //    catch
        //    {
        //    }
        //    finally
        //    {
        //        Tools.Close();
        //    }
        //}

        protected void imgbtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            OpenSISWCFServiceToolsClient Tools = new OpenSISWCFServiceToolsClient();
            string AlertMessage = "";
            try
            {
                foreach (ReorderListItem RI in rlEthnicity.Items)
                {

                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {


                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        if (chk.Checked)
                        {

                            HiddenField hdnEthnicityId = (HiddenField)RI.FindControl("hdnEthnicityId");

                           AlertMessage= Tools.DeleteEthnicity(Convert.ToInt32(hdnEthnicityId.Value));
                           if (AlertMessage != "")
                           {
                               ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + AlertMessage + "');", true);
                           }
                        }

                    }
                }

                PopulateGridview();
            }
            catch
            {
                Tools = null;
            }
        }

        protected void rlEthnicity_ItemReorder(object sender, AjaxControlToolkit.ReorderListItemReorderEventArgs e)
        {
            DataTable dt = new DataTable();

            OpenSISWCFServiceToolsClient Tool = new OpenSISWCFServiceToolsClient();
            try
            {
                dt = Tool.PopulateEthnicityDetails("", "", Convert.ToInt32(Session["SchoolID"]));
               

                if (dt != null)
                {

                    string strTempOrder = dt.Rows[e.OldIndex]["short_order"].ToString();
                    dt.Rows[e.OldIndex]["short_order"] = dt.Rows[e.NewIndex]["short_order"];
                    dt.Rows[e.NewIndex]["short_order"] = strTempOrder;
                    dt.AcceptChanges();


            
                    int Ethid = Convert.ToInt32(dt.Rows[e.NewIndex]["eth_id"].ToString());
                    string Sortorder = dt.Rows[e.NewIndex]["Short_Order"].ToString();
                    Tool.UpdateShortOrderEthnicity(Sortorder, Ethid);

                    

                    Ethid = Convert.ToInt32(dt.Rows[e.OldIndex]["eth_id"].ToString());
                    Sortorder = dt.Rows[e.OldIndex]["Short_Order"].ToString();
                    Tool.UpdateShortOrderEthnicity(Sortorder, Ethid);

                    Sortorder = null;

                    rlEthnicity.DataSource = Tool.PopulateEthnicityDetails("", "", Convert.ToInt32(Session["SchoolID"]));
                    rlEthnicity.DataBind();

                }
            }
            catch
            {
            }
            finally
            {
                Tool.Close();
            }
        }

    }
}